# Maintainer: Renato Silva <br.renatosilva@gmail.com>
# Contributor: Zach Bacon <11doctorwhocanada@gmail.com>

_realname='pidgin++'
pkgbase="mingw-w64-${_realname}-bzr"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-bzr"
pkgver=r383.16.1.0
pkgrel=1
pkgdesc='An improved version of Pidgin (mingw-w64)'
url='https://pidgin.renatosilva.net/'
license=(GPL2 partial:'GPL2+') # GPL2+, but Novell and SILC are GPL2-only
arch=('any')

options=(!libtool strip staticlibs)
source=(${_realname}::'bzr+https://code.launchpad.net/~renatosilva/pidgin++/trunk'
        001-autotools-and-fhs-pidgin.patch
        002-autotools-and-fhs-pidgin++.patch
        003-autotools-and-fhs-adaptation.patch
        004-build-fixes.patch
        005-fix-save-suffix.patch
        006-gtk-customizations.patch)
sha256sums=('SKIP'
            'e17b9e5236dc3d7e2705266181e1ffb58f2dde235e7b2b6b8333299f3ce26b66'
            '2af6a6267db17a4d4b9b72da1ed345848c85d3822c984cc3e3b0d3c2a2af82b6'
            'ff527bf67c62367b7c19ab28f152e144bd3989e035d4a0f10aa94553bbdb8f99'
            'c00ba02c209172408b7c5ffca4313ba85f6891b903d894e05406effbd1aa990a'
            'bf587810750a4f0c54a692994f5eff369cd0afd026a6f7efbda7595f64a63de6'
            '2545720a791ec20ca4e49a24a6771fa58d8170b6c33a22b35fb4fcc9ce73ec1b')

conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}
           ${MINGW_PACKAGE_PREFIX}-pidgin)
provides=(${MINGW_PACKAGE_PREFIX}-${_realname}
          ${MINGW_PACKAGE_PREFIX}-pidgin
          ${MINGW_PACKAGE_PREFIX}-libpurple)
depends=(${MINGW_PACKAGE_PREFIX}-adwaita-icon-theme
         ${MINGW_PACKAGE_PREFIX}-ca-certificates
         #${MINGW_PACKAGE_PREFIX}-cyrus-sasl
         #${MINGW_PACKAGE_PREFIX}-dbus
         ${MINGW_PACKAGE_PREFIX}-drmingw
         #${MINGW_PACKAGE_PREFIX}-farstream
         ${MINGW_PACKAGE_PREFIX}-freetype
         ${MINGW_PACKAGE_PREFIX}-fontconfig
         ${MINGW_PACKAGE_PREFIX}-gettext
         ${MINGW_PACKAGE_PREFIX}-gnutls
         ${MINGW_PACKAGE_PREFIX}-gsasl
         ${MINGW_PACKAGE_PREFIX}-gst-plugins-base
         ${MINGW_PACKAGE_PREFIX}-gst-plugins-good
         ${MINGW_PACKAGE_PREFIX}-gtk2
         ${MINGW_PACKAGE_PREFIX}-gtkspell
         ${MINGW_PACKAGE_PREFIX}-libgadu
         ${MINGW_PACKAGE_PREFIX}-libidn
         #${MINGW_PACKAGE_PREFIX}-libxml2
         ${MINGW_PACKAGE_PREFIX}-meanwhile
         #${MINGW_PACKAGE_PREFIX}-nspr
         ${MINGW_PACKAGE_PREFIX}-nss
         ${MINGW_PACKAGE_PREFIX}-ncurses
         ${MINGW_PACKAGE_PREFIX}-silc-toolkit
         ${MINGW_PACKAGE_PREFIX}-winsparkle
         ${MINGW_PACKAGE_PREFIX}-zlib)
makedepends=(gtk-doc rsync easyoptions bzr
             #${MINGW_PACKAGE_PREFIX}-dbus-glib
             ${MINGW_PACKAGE_PREFIX}-doxygen
             ${MINGW_PACKAGE_PREFIX}-gcc
             ${MINGW_PACKAGE_PREFIX}-pkg-config
             ${MINGW_PACKAGE_PREFIX}-python2
             ${MINGW_PACKAGE_PREFIX}-xmlstarlet)

_get_string_macro() {
    local -n nameref_macro="${2}"
    nameref_macro="$(grep --extended-regexp "#define\s+${2}\s" ${1})"
    nameref_macro="${nameref_macro#*\"}"
    nameref_macro="${nameref_macro%\"*}"
}

pkgver() {
    cd "${srcdir}/${_realname}"
    printf 'r%s.%s' "$(bzr revno)" "$(build/changelog.sh --version-full)"
}

prepare() {
    cd "${srcdir}/${_realname}"

    # From https://build.opensuse.org/package/view_file/windows:mingw:win32/mingw32-pidgin/pidgin-2.10.11-autotools.patch
    rm -f source/pidgin/plugins/win32/Makefile.am
    rm -f source/pidgin/plugins/win32/transparency/Makefile.am
    rm -f source/pidgin/plugins/win32/winprefs/Makefile.am
    patch -p1 < "${srcdir}/001-autotools-and-fhs-pidgin.patch"
    patch -p1 < "${srcdir}/002-autotools-and-fhs-pidgin++.patch"

    patch -p1 < "${srcdir}/003-autotools-and-fhs-adaptation.patch"
    patch -p1 < "${srcdir}/004-build-fixes.patch"
    patch -p1 < "${srcdir}/005-fix-save-suffix.patch"
    patch -p1 < "${srcdir}/006-gtk-customizations.patch"

    cd source
    libtoolize --force --copy --install
    autoreconf -f -i
}

build() {
    msg2 'Synchronizing build directory'
    rsync --recursive --times "${srcdir}/${_realname}/source"/* "${srcdir}/${_realname}.build.${CARCH}"
    cd "${srcdir}/${_realname}.build.${CARCH}"

    case "${CARCH}" in
        x86_64) APPLICATION_BITNESS='64' ;;
        i686)   APPLICATION_BITNESS='32' ;;
        *)      APPLICATION_BITNESS='BITNESS'
    esac
    _get_string_macro 'libpurple/internal.h' APPLICATION_NAME
    _get_string_macro 'libpurple/internal.h' APPLICATION_WEBSITE

    sed -i "s#env python#env python2#" */plugins/*.py
    sed -i "s#env python#env python2#" libpurple/purple-{remote,notifications-example,url-handler}
    sed -i "s/@APPLICATION_BITNESS@/${APPLICATION_BITNESS:-bitness}/g" */win32/*.rc.in
    sed -i "s/@APPLICATION_WEBSITE@/${APPLICATION_WEBSITE//\//\\\/}/g" */win32/*.rc.in
    sed -i "s/@APPLICATION_NAME@/${APPLICATION_NAME}/g" */win32/*.rc.in

    CPPFLAGS+=' -DENABLE_UPDATE_CHECK'
    CPPFLAGS+=' -D_WIN32_WINNT=0x501 -DHAVE_GETADDRINFO' # Enable IPv6

    INTLTOOL_PERL=/usr/bin/perl ./configure \
        --prefix=${MINGW_PREFIX} \
        --build=${MINGW_CHOST} \
        --host=${MINGW_CHOST} \
        --sysconfdir=${MINGW_PREFIX}/etc \
        --with-python=${MINGW_PREFIX}/bin/python2 \
        --with-system-ssl-certs=${MINGW_PREFIX}/ssl/certs \
        --without-x \
        --disable-avahi \
        --disable-schemas-install \
        --disable-screensaver \
        --disable-perl \
        --disable-tcl \
        --disable-tk \
        --disable-sm \
        --disable-nm \
        --disable-vv \
        --disable-dbus \
        #--enable-cyrus-sasl

    make
}

package() {
    cd "${srcdir}/${_realname}.build.${CARCH}"
    make DESTDIR="${pkgdir}" install
    install -Dm644 COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"

    # GTK+ customizations
    cp -r "${srcdir}/${_realname}/source/pidgin/win32/gtk"/* "${pkgdir}${MINGW_PREFIX}"
    cd "${pkgdir}${MINGW_PREFIX}/share/themes"
    mv MS-Windows "${APPLICATION_NAME}"

    # Changelog
    mkdir -p "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"
    cd "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"
    "${srcdir}/${_realname}/build/changelog.sh" --html --screenshot-prefix '../../' --output 'CHANGELOG.html'
}
